package com.distributor;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

public class ActivityMenu extends Activity 
{

	//Button btnAgent,btnDistributor,btnMyAccount,btnFav,btnSettings,btnSalesAgent;
	Button btnBack,btnLogOut;
	GridView menu;
	ArrayList<Integer> servicesImage;
	ArrayList<String> servicesName;
	AlertDialog alert;
	String AGENT="Agent",DISTRIBUTOR="Distributor",MYACCOUNT="My Account",SETTINGS="Settings",SALESAGENT="Sales Agent";
			
			
 	
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		
		new AlertDialog.Builder(ActivityMenu.this)
        .setTitle( "Exit Application." )
        .setMessage( "Are you sure you want to Exit ?" )
        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //do stuff onclick of YES
            	finish();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
			}
        })
        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface arg0, int arg1)
            {
                //do stuff onclick of CANCEL
            	arg0.dismiss();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			}
        }).show();
	}
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        btnLogOut=(Button)findViewById(R.id.btnLogout); 
		btnBack=(Button)findViewById(R.id.btnPrevious);
		
		menu=(GridView)findViewById(R.id.menuView);
		btnBack.setVisibility(Button.INVISIBLE);
		
		servicesName=new ArrayList<String>();
		servicesImage=new ArrayList<Integer>();
		if((AppData.getUserInfo().getMaxLevel()-AppData.getUserInfo().getResellerLevel()==0))
        {
			servicesName.add(MYACCOUNT);
			servicesName.add(SETTINGS);
			 
			servicesImage.add(R.drawable.my_account);
			servicesImage.add(R.drawable.help);
        }
		else if((AppData.getUserInfo().getMaxLevel()-AppData.getUserInfo().getResellerLevel()==1))
	    {
	        	servicesName.add(AGENT);
	    		servicesName.add(MYACCOUNT);
	    		servicesName.add(SETTINGS);
	    		
	    		servicesImage.add(R.drawable.agent);
	    		servicesImage.add(R.drawable.my_account);
	    		servicesImage.add(R.drawable.help);
	    		
	    }
		else if((AppData.getUserInfo().getMaxLevel()-AppData.getUserInfo().getResellerLevel()>=2))
		{
			servicesName.add(AGENT);
			servicesName.add(DISTRIBUTOR);
			servicesName.add(MYACCOUNT);
			servicesName.add(SETTINGS);
			
			servicesImage.add(R.drawable.agent);
			servicesImage.add(R.drawable.distributor);
			servicesImage.add(R.drawable.my_account);
			servicesImage.add(R.drawable.help);
		}
		if(AppData.getUserInfo().getAccountType().equalsIgnoreCase("p"))
        {
			servicesName.add(0, SALESAGENT);
			servicesImage.add(0,R.drawable.sales_assistant);

        }
		menu.setAdapter(new ImageAdapter(this,servicesName,servicesImage));
		
		 btnLogOut.setOnClickListener(new OnClickListener()
		 {
			
			public void onClick(View v)
			{
				  new AlertDialog.Builder(ActivityMenu.this)
			        .setTitle( "Exit Application" )
			        .setMessage( "Are you sure you want to Logout?" )
			        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
			        {
			            public void onClick(DialogInterface arg0, int arg1) 
			            {
			                //do stuff onclick of YES
			                finish();
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
							
						}
			        })
			        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface arg0, int arg1)
			            {
			                //do stuff onclick of CANCEL
			            	arg0.dismiss();
			               
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
						}
			        }).show();
		  	}
			
		 });
        /*btnAgent=(Button)findViewById(R.id.btnAgent);
        btnDistributor=(Button)findViewById(R.id.btnDist);
        btnMyAccount=(Button)findViewById(R.id.btnMyAccount);
        btnFav=(Button)findViewById(R.id.btnFav);
        btnSettings=(Button)findViewById(R.id.btnSettings);
        btnSalesAgent=(Button)findViewById(R.id.btnSalesAgent);
        
        if((AppData.getUserInfo().getMaxLevel()-AppData.getUserInfo().getResellerLevel()==0))
        {
        	btnAgent.setVisibility(Button.GONE);
        	btnDistributor.setVisibility(Button.GONE);
        }
        else if((AppData.getUserInfo().getMaxLevel()-AppData.getUserInfo().getResellerLevel()==1))
        {
        	btnDistributor.setVisibility(Button.GONE);
        }
         
        if(AppData.getUserInfo().getAccountType().equalsIgnoreCase("p"))
        {
        	btnSalesAgent.setVisibility(Button.VISIBLE);
        }
        btnAgent.setOnClickListener(new OnClickListener()
        {
			
			public void onClick(View arg0) 
			{
				
				if(AppData.getUserInfo().getAccountType().equalsIgnoreCase("s"))
				{
					startActivity(new Intent(ActivityMenu.this,ManageAgentActivity.class).putExtra("nextscreen", "agent"));
				}
				else
				{
					alert=createDialog(new CharSequence[]{"New Agent","Manage Agent"},"agent");
					alert.show();
	  	
				}
				
			}
		});
        
        btnDistributor.setOnClickListener(new OnClickListener()
        {
			
			public void onClick(View v) 
			{
				if(AppData.getUserInfo().getAccountType().equalsIgnoreCase("s"))
				{
					startActivity(new Intent(ActivityMenu.this,ManageAgentActivity.class).putExtra("nextscreen", "distributor"));
				}
				else
				{
					alert=createDialog(new CharSequence[]{"New Distributor","Manage Distributor"},"distributor");
					alert.show();

				}
				
			}
		});
        btnSalesAgent.setOnClickListener(new OnClickListener()
        {
			
			public void onClick(View arg0) 
			{  
				AlertDialog.Builder builder=new AlertDialog.Builder(ActivityMenu.this);
				builder.setItems(new CharSequence[]{"New Sales Agent","Manage Sales Agent"}, new DialogInterface.OnClickListener() 
				{ 
					public void onClick(DialogInterface dialog, int position) 
					{
						if(position==0)
						{
							startActivity(new Intent(ActivityMenu.this,ActivitNewSalesAgent.class));
						}
						else
						{
							startActivity(new Intent(ActivityMenu.this,ManageAgentActivity.class).putExtra("nextscreen", "salesagent"));
						}
						
					}
				});
				AlertDialog alert=builder.create();
				alert.show();
			}
		});
 
        
        btnMyAccount.setOnClickListener(new OnClickListener()
        {
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				startActivity(new Intent(ActivityMenu.this,MyAccountActivity.class));
			//	Toast.makeText(MenuActivity.this,"My Account",Toast.LENGTH_SHORT).show();
			}
		});
        
        btnFav.setOnClickListener(new OnClickListener()
        {
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				Toast.makeText(ActivityMenu.this,"Conming Soon..",Toast.LENGTH_SHORT).show();
			}
		});
        
        btnSettings.setOnClickListener(new OnClickListener()
        {
			
			public void onClick(View arg0) 
			{
				startActivity(new Intent(ActivityMenu.this,ActivitySettings.class));
			}
		});*/
        
    }
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
	    if(resultCode==2 && requestCode==11)
	    {
	        finish();
	    }
	}
	public class ImageAdapter extends BaseAdapter 
	{
	    private Context mContext;
	     
	    ArrayList<String> services;
	    ArrayList<Integer> images;
	    AlertDialog alert;
	 
		public ImageAdapter(Context c,
				ArrayList<String> servicesName, ArrayList<Integer> servicesImage)
		{
			mContext = c;
			this.services=servicesName;
			this.images=servicesImage;
	    	 
		}

		public int getCount() 
	    {
	        return services.size();
	    }

	    public Object getItem(int position) 
	    {
	        return null;
	    }

	    public long getItemId(int position) 
	    {
	        return position;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(final int position, View convertView, ViewGroup parent) 
	    { 
	    	
	        Button buttonView;
	        if (convertView == null)
	        {  // if it's not recycled, initialize some attributes
	        	buttonView = new Button(mContext);
	        	buttonView.setLayoutParams(new GridView.LayoutParams(110, 110)); 
	        	buttonView.setPadding(8, 8, 8, 8);
	        }
	        else
	        {
	        	buttonView = (Button) convertView;
	        }
	        
	        //buttonView.setBackgroundResource(services[position]);
	        buttonView.setCompoundDrawablesWithIntrinsicBounds(0, images.get(position), 0,0);
	        buttonView.setText(services.get(position));
	         
	        buttonView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
	        buttonView.setTextColor(android.graphics.Color.WHITE);
	         
	        buttonView.setOnClickListener(new OnClickListener() 
	        {
				
				public void onClick(View v) 
				{
					Button b=(Button)v;
					System.out.println(b.getText().toString());
					
					if(b.getText().toString().equals(SALESAGENT))
					{
						AlertDialog.Builder builder=new AlertDialog.Builder(ActivityMenu.this);
						builder.setItems(new CharSequence[]{"New Sales Agent","Manage Sales Agent"}, new DialogInterface.OnClickListener() 
						{ 
							public void onClick(DialogInterface dialog, int position) 
							{
								if(position==0)
								{
									startActivityForResult(new Intent(ActivityMenu.this,ActivitNewSalesAgent.class).putExtra("nextscreen", "Sales Agent"),11);
								}
								else
								{
									startActivityForResult(new Intent(ActivityMenu.this,ManageAgentActivity.class).putExtra("nextscreen", "Sales Agent"),11);
								}
							}
						});
						AlertDialog alert=builder.create();
						alert.show();
					}
					else if(b.getText().toString().equals(AGENT))
					{
						alert=createDialog(new CharSequence[]{"New Agent","Manage Agent"},"Agent");
						alert.show();
					}
					else if(b.getText().toString().equals(DISTRIBUTOR))
					{
						alert=createDialog(new CharSequence[]{"New Distributor","Manage Distributor"},"Distributor");
						alert.show();
					}
					else if(b.getText().toString().equals(MYACCOUNT))
					{
						startActivityForResult(new Intent(ActivityMenu.this,MyAccountActivity.class),11);
					}
					else if(b.getText().toString().equals(SETTINGS))
					{
						startActivityForResult(new Intent(ActivityMenu.this,SettingsActivity.class),11);
					}
					 
				}
			});
	        return buttonView;
	    }
	    
	}
	protected AlertDialog createDialog(CharSequence[] items, final String nextScreen) 
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(ActivityMenu.this);
		
		builder.setItems(items, new DialogInterface.OnClickListener() 
		{
			
			public void onClick(DialogInterface dialog, int position) 
			{
		
				if(position==0)
				{
					startActivityForResult(new Intent(ActivityMenu.this,ActivitNewReseller.class).putExtra("nextscreen", nextScreen),11);
				}
				else
				{
					startActivityForResult(new Intent(ActivityMenu.this,ManageAgentActivity.class).putExtra("nextscreen", nextScreen),11);
				}
			}
			
		});
		
		AlertDialog alert=builder.create();
		
		return alert;
		
	}
}
